const CompressionPlugin = require('compression-webpack-plugin')

module.exports = {
  publicPath: '',
  productionSourceMap: false,
  css: {
    // 启用 CSS modules
    requireModuleExtension: true,
    // 是否使用css分离插件
    extract: true,
    // 开启 CSS source maps，一般不建议开启
    sourceMap: false,
    loaderOptions: {
      stylus: {
        'resolve url': true,
        import: []
      },
      sass: {
        data: `
          @import "@/assets/style/_variable.scss";
        ` // scss变量
      },
      postcss: {
        plugins: [
          require('postcss-pxtorem')({
            rootValue: 25, // 换算的基数
            selectorBlackList: ['weui', 'mu'], // 忽略转换正则匹配项
            propList: ['*']
          })
        ]
      }
    }
  },
  devServer: {
    proxy: {
      '/host': {
        target: 'http://zbshop.com/api', // 要访问的那个url.
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/host': '/'
        }
      },
      '/home': {
        target: 'http://zbshop.com/home', // 要访问的那个url.
        changeOrigin: true,
        ws: true,
        pathRewrite: {
          '^/home': '/'
        }
      }
    }
  },
  outputDir: '../public/mobile',
  chainWebpack: config => {
    // 移除预加载组件
    config.plugins.delete('prefetch')
    // 添加分析工具
    if (process.env.NODE_ENV === 'production') {
      if (process.env.npm_config_report) {
        config
          .plugin('webpack-bundle-analyzer')
          .use(require('webpack-bundle-analyzer').BundleAnalyzerPlugin)
          .end()
      }
    }
  },
  configureWebpack: config => {
    if (process.env.NODE_ENV === 'production') {
      // 为生产环境修改配置...
      config.mode = 'production'
      return {
        plugins: [
          new CompressionPlugin({
            test: /\.js$|\.html$|\.css/, //匹配文件名
            threshold: 10240, //对超过10k的数据进行压缩
            deleteOriginalAssets: false //是否删除原文件
          })
        ],
        CommonsChunkPlugin: {
          minChunks: 2
        }
      }

    }
  }
}
